﻿Library.UserInterface.Widgets.Calculator = function (options) {
    var defaults = {
        container: null,
        loader: "Yükleniyor...",
        titleSelector: ""
    };
    var options = $.extend(defaults, options);

    this.container = options.container;
    this.loader = options.loader;
    this.titleSelector = options.titleSelector;

    this.initialize(false);
}

Library.UserInterface.Widgets.Calculator.prototype = {
    container: null,
    loader: "",
    titleSelector: "",
    left: false,
    right: false,
    result: 0,
    operator: "+",
    fullscreen: false,
    minimize: true,
    settings: false,
    id: null,
    type: "widget.calculator",

    initialize: function (set) {
        if (this.id != null)
            Library.UserInterface.Widgets.Dispose(this.id);

        this.id = Library.Util.ReplaceAll(Library.Crypto.Guid(), "-", "");
        this.container.find(".portlet-widget").html(this.loader);

        Library.UserInterface.Widgets.Add(this);

        if (this.titleSelector == "")
            this.titleSelector = this.container.find(".portlet-title").selector;

        if (this.titleSelector != "")
            $(this.titleSelector).html(this.loader);

        try {
            $(this.titleSelector).html("Hesap Makinesi");

            var html = "";
            html += "<div class=\"ui-calc\">";
            html += "<div class=\"ui-calc-result\">0</div>";
            html += "<div class=\"ui-calc-digits\">";
            html += "  <ul><li>7</li><li>4</li><li>1</li><li>0</li></ul>";
            html += "  <ul><li>8</li><li>5</li><li>2</li><li>.</li></ul>";
            html += "  <ul><li>9</li><li>6</li><li>3</li><li>=</li></ul>";
            html += "  <ul><li>+</li><li>-</li><li>*</li><li>/</li></ul>";
            html += "</div>";
            html += "</div>";
            this.container.find(".portlet-widget").html(html);
            var digits = this.container.find('.ui-calc-digits ul li');
            var widget = this;
            var leftBuffer = "";
            var rightBuffer = "";

            digits.click(function () {
                try {
                    var key = $(this).text();
                    if (Library.Validation.IsNumeric(key, "")) {
                        if (widget.left) {
                            rightBuffer += key.toString();
                            widget.container.find(".ui-calc-result").text(rightBuffer);
                        } else {
                            leftBuffer += key.toString();
                            widget.container.find(".ui-calc-result").text(leftBuffer);
                        }
                    } else if (key != '=' && key != '.') {
                        widget.operator = key;
                        widget.left = leftBuffer;
                    } else if (key == '.') {
                        if (widget.left) {
                            rightBuffer += '.';
                            widget.container.find(".ui-calc-result").text(rightBuffer);
                        } else {
                            leftBuffer += '.';
                            widget.container.find(".ui-calc-result").text(leftBuffer);
                        }
                    } else {
                        widget.right = rightBuffer;
                        if (widget.left.substring(widget.left.length - 1, 1) == '.')
                            widget.left += '0';
                        if (widget.right.substring(widget.right.length - 1, 1) == '.')
                            widget.right += '0';

                        widget.calculate();
                    }
                } catch (err) {
                    Library.UserInterface.Widgets.Error(widget.id, err); 
                }
            });

            Library.UserInterface.Widgets.SetHeaderIcons(this);
            this.container.find(".portlet-content").css("max-height", "250px");
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err);
        }
    },

    calculate: function () {
        try {
            this.result = eval(this.left + this.operator + this.right);
            this.container.find(".ui-calc-result").text(this.result);
            this.left = null;
            this.right = null;
            leftBuffer = '';
            rightBuffer = '';
        } catch (err) {
            Library.UserInterface.Widgets.Error(this.id, err);
        }
    }
}